package chap07;

import java.io.*;
import java.util.Random;

public class File_demo {
    File file = new File("E:\\JAVA\\java_demo\\public\\file", "java_show.txt");
    File file2 = new File("E:\\JAVA\\java_demo\\public\\file", "java_show_2.txt");
    String file_path = "E:\\JAVA\\java_demo\\public\\file\\java_show.txt";

    public static void main(String[] args) {
        File_demo file = new File_demo();
//        file.file();
//        file.FileInputStream_demo();
//        file.FileOutPutStream();
//          file.DatainputStream();
//            file.BufferedInputStream();
//            file.BufferedOutputStream();
//            file.ObjectOutputStream();
//        file.ObjectinputStream();
//        file.FileReaderAndFileWriter();
//        file.InputStreamReader();
//         file.InputStreamReader();
//          file.BufferedReader_BufferedWriter();
//            file.PrintWriter();
            file.RandomAccessFile();
    }

    public void file() {
        System.out.println(this.file.getName());
        System.out.println(this.file.exists());
        //相对路径
        System.out.println(this.file.getPath());
        //绝对路径
        System.out.println(this.file.getAbsolutePath());
        System.out.println(this.file.canRead());
        System.out.println(this.file.canWrite());
        System.out.println(this.file.length() + "b");
    }

    public void FileInputStream_demo() {
        try {
            byte bytes[] = new byte[512];
            //开启该文件的 输入流
            FileInputStream fis = new FileInputStream(this.file);
            int rs = 0;
            System.out.println("input content of file:");
            //不断的去读取 再输出 and deposit into array of byte
            while ((rs = fis.read(bytes, 0, 512)) > 0) {
                String s = new String(bytes, 0, rs);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void FileOutPutStream() {
        int b;
        byte bytest[] = new byte[512];

        System.out.println("show you input of text");
        try {
            if (!file.exists())
                file.createNewFile();//创建新的
            b = System.in.read(bytest); //不行啊
            FileOutputStream fos = new FileOutputStream(file, true);
            fos.write(bytest, 0, b);
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void DatainputStream() {
        try {
            FileOutputStream fout = new FileOutputStream(this.file_path);
            DataOutputStream dfout = new DataOutputStream(fout);
            for (int i = 0; i < 6; i++) {
                dfout.writeInt(i);  //写入了数字 那我如果写入字符串或者其他东西会怎么样，为什么无法显示该区域的东西
            }
            //关闭  DataOutputStream 流  试用流的时候去关闭省去应该的资源
            dfout.close();
            FileInputStream fin = new FileInputStream(this.file_path);
            DataInputStream dfin = new DataInputStream(fin);
            for (int i = 0; i < 6; i++) {
                System.out.print(dfin.readInt() + ",");
            }
            dfin.close();
        } catch (Exception e) {
            System.out.println("e");
            e.printStackTrace();
        }
    }

    public void BufferedInputStream() {
        FileInputStream fis = null;//启动 fileInputStream
        BufferedInputStream bis = null;//buffered  做为容器
        try {
            fis = new FileInputStream(this.file);//套入对应的内容
            bis = new BufferedInputStream(fis);
            byte[] buf = new byte[30];
            int len = 0;
            while ((len = bis.read(buf)) != -1) {
                String s = new String(buf, 0, len);
                System.out.print(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void BufferedOutputStream() {
        FileOutputStream fos = null;//启动 fileInputStream
        BufferedOutputStream bos = null;//buffered  做为容器
        try {
            fos = new FileOutputStream(this.file);
            bos = new BufferedOutputStream(fos);
            String s = "java是试用最广的开发语言";
            bos.write(s.getBytes());
            bos.flush();//清空缓冲区
            fos.close();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void ObjectinputStream() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(this.file);
            ois = new ObjectInputStream(fis);
            User user1 = (User) ois.readObject();  //readObject 读取文本对象 或许读取异地所指针会往下一指
            System.out.println("name:" + user1.getName() + "--sex:" + user1.getSex() + "--age:" + user1.getAge());
            User user2 = (User) ois.readObject();
            System.out.println("name:" + user2.getName() + "--sex:" + user2.getSex() + "--age:" + user2.getAge());
            fis.close();
            ois.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex) {//一定有这个报错才能使用ObjectInputStream 流
            ex.printStackTrace();
        } catch (ClassNotFoundException exx) {//没有发现没有的类的错误
            exx.printStackTrace();
        }
    }

    public void ObjectOutputStream() {
        FileOutputStream fis = null;
        ObjectOutputStream oos = null;
        try {
            fis = new FileOutputStream(this.file);//out 只能用Out的容器
            oos = new ObjectOutputStream(fis);
            User user1 = new User();  //修改 user不能和类 同名？
            user1.setName("Simon");
            user1.setSex("男");
            user1.setAge(28);
            oos.writeObject(user1);
            User user2 = new User();
            user2.setName("conlin");
            user2.setSex("女");
            user2.setAge(26);
            oos.writeObject(user2);
            oos.flush();
            fis.close();
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex) {//一定有这个报错才能使用ObjectInputStream 流
            ex.printStackTrace();
        }
    }

    public void PrintStream() {
        //print 类有点熟悉是不是 是不是感觉System.out.println这个很像
        PrintStream ps;
        try {
            File file=this.file;
            if(!file.exists()){
                file.createNewFile();
            }
            ps = new PrintStream(new FileOutputStream(file));
            Random r=new Random();
            int rs;
            for(int i=0;i<5;i++)
            {
                rs=r.nextInt(100);
                ps.println(rs+"\n");
            }
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void FileReaderAndFileWriter() {
        try {
            //不用试用FileOutinput
            FileWriter writer = new FileWriter(this.file);
            writer.write("今天非常开心");
            writer.close();
            FileReader reader = new FileReader(this.file);
            //for（ 初始值 条件值 条件变化值
            for (int c = reader.read(); c != -1; c = reader.read()) {
                System.out.print((char) c);
            }
        } catch (IOException e) {
            System.out.println("异常" + e);
            e.printStackTrace();
        }
    }

    public void InputStreamReader() {
        try {
            int rs;
            File file = this.file;
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            System.out.println("The content of 啊 卧槽這是inputStreamReader 测试结果:");
            while ((rs = isr.read()) != -1) {//顺序读取文件里的内容并且给值
                System.out.println((char) rs);
            }
            isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void OutputSTream() {
        File filein = this.file;
        File fileout = this.file2;
        FileInputStream fis;
        try {
            if (!filein.exists()) {
                filein.createNewFile();
            }
            if (!fileout.exists()) {
                fileout.createNewFile();
            }
            fis = new FileInputStream(filein);
            FileOutputStream fos = new FileOutputStream(fileout, true);// true 意思是 有数据就在后面追加数据 false  就是把源文件覆盖写一次
            InputStreamReader in = new InputStreamReader(fis);
            OutputStreamWriter out = new OutputStreamWriter(fos);
            int is;
            while ((is = in.read()) != -1) {
                out.write(is);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void BufferedReader_BufferedWriter(){
        try{
            FileReader fr;
            fr=new FileReader(this.file_path);
            File file=this.file;
            FileWriter fos=new FileWriter(file);
            BufferedReader br=new BufferedReader(fr);
            BufferedWriter bw=new BufferedWriter(fos);
            String str=null;//？什么意思 就专门给他加个回车？
            while ((str = br.readLine()) != null) {
                bw.write(str+"\n");
            }
            br.close();
            bw.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void PrintWriter(){
        File filein = this.file;
        File fileout = this.file2;
        try {
            BufferedReader br = new BufferedReader((new FileReader((filein))));//FileReader真的还是个爹啊
            PrintWriter pw = new PrintWriter(new FileWriter((fileout)));//哥找个爹
            int b;
            while ((b = br.read()) != -1) {
                pw.print(b);//写入文件？       实现文件复制 写入之后 发现变成了了ascii
                pw.println();
            }
            br.close();;
            pw.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void RandomAccessFile(){//新的强者？
        int bytes[]={1,2,3,4,5};
        try{
        RandomAccessFile raf=new RandomAccessFile(this.file_path,"rw");//写入权限
            for(int i=0;i<bytes.length;i++)
            {
                raf.writeInt(bytes[i]);//遍历直接 每个类型对应不同的方法
            }
            for(int i=bytes.length-1;i>0;i--)//反过来输出？
            {
                    raf.seek(i*4);//申请变量？  设置文件指针位置
                
                System.out.println(raf.readInt());
            }
            raf.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


}

class User implements Serializable {     //要试用
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


